var API = {
    params: {
        /**matomo API接口必须参数 */
        "token_auth": piwik.token_auth,
        "force_api_session": 1,
        "format": 'json'
    },
    _getFormData: function (param) {
        var formData = new FormData();
        for (var p in param) {
            
            //处理 值为对象 时再循环一次 变成PHP格式数组
            var first = param[p];
            if(first === 'null' || first == null || first == undefined || first === 'undefined'){
                formData.append(p, "");
            }
            // console.log(p, typeof first )
            else if (typeof first === 'object') {
                //发送 数组 例如 images:[{url:'', name:''}]
                if (first.length > 0) {
                    for (var j in first) {

                        if (typeof first[j] === 'object') {
                            for (var k in first[j]) {
                                formData.append(p + '[' + j + '][' + k + ']', first[j][k]);
                            }
                        } else {
                            formData.append(p, first);
                        }
                    }
                } else {
                    // 发送对象为数组 例如 file:{ name:'', url:''}
                    for (var i in first) {
                        formData.append(p + '[' + i + ']', API._filter_value(first[i]));
                    }
                }

            } else {
                formData.append(p, API._filter_value(first));
            }

        }
        return formData;
    },
    // 把一维数组作为‘,’拼接的字符串
    _filter_value(value) {
        if (typeof value === 'Array') {
            return value.join(',');
        }
        return value;
    },
    // 发送axios请求
    _axiosPost: function (url, param) {
        axios.defaults.withCredentials = true;
        return new Promise((resolve, reject) => {
            axios.post(url,
                API._getFormData(param)
            ).then((response) => {
                resolve(response.data);
            }).catch(ret => {
                reject(ret);
            })
        })
    },
    /**
     * options: { 
            'startdate': startdate,
            'nowdate': nowdate           
        }
     * @param {*} startdate 
     * @param {*} nowdate 
     * @returns 
     */
    AdsPlaceReportOverview(options ){
        var param = Object.assign(options, { format: "json" });
        return API._axiosPost('index.php?module=AdsPlace&action=reportOverview', param);  
    },
    /**
     *options =  {
            'startdate': startdate,
            'nowdate': nowdate,
            'days': days
        }
     * @param { } startdate 
     * @param {*} nowdate 
     * @param {*} days 
     * @returns 
     */
    reportAdsPlaceRange(options) {
        var param = Object.assign(options, { format: "json" });
        return API._axiosPost('index.php?module=AdsPlace&action=reportAdsPlaceRange', param);
    },
    adverteOverview(options) {
        var param = Object.assign(options, { format: "json" });
        return API._axiosPost('index.php?module=AdsPlace&action=adverteOverview', param);
    },
    mediasOverview(options) {
        var param = Object.assign(options, { format: "json" });
        return API._axiosPost('index.php?module=AdsPlace&action=mediasOverview', param);
    },

    /**
     * 倒计时计算
     */
    setTimeData(vue, cm) {
        // console.log(cm, '--cm');
        vue.hour = Math.floor((cm / 1000 / 60 / 60) % 24);
        vue.hour =
            vue.hour.toString().length == 1 ? "0" + vue.hour : vue.hour;
        vue.minute = Math.floor((cm / 1000 / 60) % 60);
        vue.minute =
            vue.minute.toString().length == 1 ? "0" + vue.minute : vue.minute;
        vue.seconds = Math.floor((cm / 1000) % 60);
        vue.seconds =
            vue.seconds.toString().length == 1
            ? "0" + vue.seconds
            : vue.seconds;
    },
    timemer : null,
    timeCountDown(vue, cm, endCallback){
        
        if (cm >= 0) {
            vue.hour = Math.floor((cm / 1000 / 60 / 60) % 24);
            vue.hour =
                vue.hour.toString().length == 1 ? "0" + vue.hour : vue.hour;
            vue.minute = Math.floor((cm / 1000 / 60) % 60);
            vue.minute =
                vue.minute.toString().length == 1
                ? "0" + vue.minute
                : vue.minute;
            vue.seconds = Math.floor((cm / 1000) % 60);
            vue.seconds =
                vue.seconds.toString().length == 1
                ? "0" + vue.seconds
                : vue.seconds;
           
           API.timemer =  setTimeout(function () {
                cm -= 1000;
                API.timeCountDown(vue, cm, endCallback);
            }, 1000);
        }else{
            endCallback()
        }
    }
}

export default API;